class Solution {
public:
    int maxScore(vector<int>& cardPoints, int k) {
        const int INF=0x3f3f3f3f;
        int min_points=INF,sum=0;
        int n=cardPoints.size(),_k=n-k;
        for(int left=0,right=0;right<n;++right)
        {
            sum+=cardPoints[right];
            if(left<=right&&right-left+1>_k)
            {
                sum-=cardPoints[left++];
            }
            if(right-left+1==_k) min_points=min(min_points,sum);
        }
        return accumulate(cardPoints.begin(),cardPoints.end(),0)-min_points;
    }
};